package cn.edu.sdyu.ie.crs.api;

import cn.edu.sdyu.ie.crs.common.response.PageVO;
import cn.edu.sdyu.ie.crs.common.response.Result;
import cn.edu.sdyu.ie.crs.domain.Classroom;
import cn.edu.sdyu.ie.crs.domain.MaintenanceRecord;
import cn.edu.sdyu.ie.crs.service.ClassroomService;
import cn.edu.sdyu.ie.crs.service.MaintenanceRecordService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.v3.oas.annotations.Operation;
//import io.swagger.v3.oas.annotations.Parameter;
//import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "维修记录管理", description = "维修记录信息管理接口")
@Slf4j
@RestController
@RequestMapping("api/maintenanceRecord")
public class MaintenanceRecordController {

    @Autowired
    private MaintenanceRecordService maintenanceRecordService;

    /**
     * 添加新维修记录信息
     *
     * @param maintenanceRecord
     * @return
     */
    @Operation(tags = "维修记录管理", summary = "添加维修记录信息")
    @PostMapping("")
    public Result<Void> addMaintenanceRecord(MaintenanceRecord maintenanceRecord) {

        if (maintenanceRecord.getId() != null && !maintenanceRecord.getId().equals(0)) {
            log.warn("请求ID参数，若更新对象，请使用PUT方式发起请求。");
            return Result.failure();
        }
        maintenanceRecordService.saveMaintenanceRecord(maintenanceRecord);
        return Result.success();
    }

    /**
     * 更新维修记录信息
     *
     * @param maintenanceRecord
     * @return
     */
    @Operation(tags = "维修记录管理", summary = "修改维修记录信息")
    @PutMapping("")
    public Result<Object> updateMaintenanceRecord(MaintenanceRecord maintenanceRecord) {

        if (maintenanceRecord.getId() == null || maintenanceRecord.getId().equals(0)) {
            log.warn("未请求ID参数，使用POST方式创建对象。当前不会更新任何对象");
            return Result.failure();
        }
        maintenanceRecordService.saveMaintenanceRecord(maintenanceRecord);
        return Result.success();
    }

    /**
     * 删除指定ID的维修记录信息
     *
     * @param id
     * @return
     */
    @Operation(tags = "维修记录管理", summary = "删除维修记录信息")
    @DeleteMapping("/{id}")
    public Result<Void> deleteMaintenanceRecordById(@PathVariable Integer id) {
        maintenanceRecordService.deleteMaintenanceRecordById(id);
        return Result.success();
    }

    /**
     * 查询指定ID的维修记录信息
     *
     * @param id
     * @return
     */
    @Operation(tags = "维修记录管理", summary = "查询指定ID的维修记录信息")
//    @Parameters(value = {
//            @Parameter(description = "教室ID")
//    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "维修记录ID")
    })
    @GetMapping("/{id}")
    public Result<MaintenanceRecord> findMaintenanceRecordById(/*@Parameter(description = "教室ID")*/ @PathVariable Integer id) {
        MaintenanceRecord maintenanceRecord = maintenanceRecordService.findMaintenanceRecordById(id);

        if (maintenanceRecord == null) {
            return Result.failure();
        }

        return Result.success(maintenanceRecord);
    }

    /**
     * 分页查询维修记录信息
     * @param page
     * @param pageSize
     * @return
     */
    @Operation(tags = "维修记录管理", summary = "分页查询维修记录信息")
    @GetMapping("")
    public Result<PageVO<MaintenanceRecord>> listMaintenanceRecordsPage(@RequestParam(defaultValue = "1") Integer page,
                                                        @RequestParam(defaultValue = "10") Integer pageSize) {

        Page<MaintenanceRecord> list = maintenanceRecordService.findByPage(page - 1, pageSize);
        PageVO<MaintenanceRecord> pageVO = PageVO.getPageVO(list);
        return Result.success(pageVO);
    }

    /**
     * 根据用户名查询维修记录信息
     * @param username
     * @return
     */
    @Operation(tags = "维修记录管理", summary = "根据用户名查询维修记录信息")
    @GetMapping("/user")
    public Result<List<MaintenanceRecord>> listMaintenanceRecordsByUsername(@RequestParam String username) {
        List<MaintenanceRecord> list = maintenanceRecordService.findMaintenanceRecordByUsername(username);
        return Result.success(list);
    }

}

